package com.kotiln.lgc.androidkotlindsl.activity

import android.Manifest
import android.os.Build
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import com.kotiln.lgc.androidkotlindsl.R
import com.kotiln.lgc.lib_base.base.BaseActivity
import com.kotiln.lgc.lib_base.helper.ARouterHelper
import com.kotiln.lgc.lib_base.utils.ParamsUtils
import com.kotiln.lgc.lib_network.HttpManager
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.custom.MyPopupWindow
import com.lxj.xpopup.interfaces.SimpleCallback
import com.yanzhenjie.permission.Action
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * 主App 空壳
 */
class MainActivity : BaseActivity(), View.OnClickListener {

    val instance by lazy { this }

    private val TAG: String = "MainActivity"

    var popupView: MyPopupWindow? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun getTitleText(): String {
        return getString(R.string.app_name)
    }

    override fun isShowBack(): Boolean {
        return true
    }

    override fun destoryData() {
    }

    override fun initView() {
    }


    override fun initData() {
        //沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        //动态权限 某一权限
        if(checkPermission(Manifest.permission.RECORD_AUDIO)){
            //获得权限后的操作
        }else{
            //未获取权限后的操作
            requestPermission(arrayOf(Manifest.permission.RECORD_AUDIO), Action<List<String>> {
                //
            })
        }

        btnPay.setOnClickListener(this)
        btnGoodsLists.setOnClickListener(this)
        btnRetrofitGet.setOnClickListener(this)
        btnRetrofitPost.setOnClickListener(this)
        btnRetrofitGetHeader.setOnClickListener(this)
        btnRetrofitPostHeader.setOnClickListener(this)
        btnPopupWindow.setOnClickListener(this)
    }



    override fun onClick(view: View?) {
        when (view?.id) {
//            R.id.btnPay ->ARouterHelper.startActivity(ARouterHelper.PATH_APP_PAY)
            R.id.btnPay ->ARouterHelper.startActivity(ARouterHelper.PATH_APP_PAY,"data","传过来的数据")
            R.id.btnGoodsLists -> ARouterHelper.startActivity(ARouterHelper.PATH_APP_GOODS_LIST)
            //Retrofit Get请求
            R.id.btnRetrofitGet -> getRequest()
            //Retrofit Post请求
            R.id.btnRetrofitPost -> postRequest()
            R.id.btnRetrofitGetHeader -> getHeaderRequest()
            R.id.btnRetrofitPostHeader -> postHeaderRequest()
            //popupwindow 弹框
            R.id.btnPopupWindow -> popupWindow(view)
        }
    }

    fun popupWindow(view: View){
        XPopup.setAnimationDuration(100);
        if (popupView == null) {
            popupView = XPopup.Builder(instance)
                .atView(view)
                .isClickThrough(true) //                    .dismissOnTouchOutside(false)
                //                    .isCenterHorizontal(true)
                .autoOpenSoftInput(true) //                    .offsetY(-150)
                //                    .offsetX(100)
                //                .dismissOnTouchOutside(false)
                .setPopupCallback(object : SimpleCallback() {
                    override fun onShow(popupView: BasePopupView) {
                        Toast.makeText(instance, "显示了", Toast.LENGTH_SHORT).show()
                    }

                    override fun onDismiss(popupView: BasePopupView) {}
                })
                .asCustom(MyPopupWindow(instance)) as MyPopupWindow
    }

        val location = IntArray(2)
        btnPopupWindow.getLocationOnScreen(location)
        val x = location[0]
        val y = location[1]
        popupView!!.toggle()
     }


    fun getRequest() {
        val mach = "99219136"
        HttpManager.queryAppId(mach).enqueue(object : Callback<ResponseBody> {
            override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                Log.i(TAG, "message:${response.body()?.string()}")
            }

            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                Log.i(TAG, "t:${t.toString()}")
            }
        })
    }
    fun postRequest() {
        val hashmap= mutableMapOf<String, Any>()
        val requestBody = ParamsUtils.getRequestBody(hashmap)
        HttpManager.getKindInd(requestBody).enqueue(object : Callback<ResponseBody> {
            override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                Log.i(TAG, "message:${response.body()?.string()}")
            }

            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
            }

        })
    }
    fun getHeaderRequest() {}
    fun postHeaderRequest() {}


}